package org.itboys.mobile.service.mongo.weixin.account;

import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mdl.entity.mongo.account.SundayAccount;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.service.mongo.mobile.common.SundayMobileCodeService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2017/11/30.
 * @author 朱坤涛
 */
@Service
public class SundayWeixinAccountService extends BaseMongoService<SundayAccount> {
    @Autowired
    private SystemDictionaryService dicService;
    @Autowired
    private SundayMobileCodeService codeService;
    @Resource(name="mobileDs")
    private MongoDataSource mongoDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mongoDataSource;
    }
    @Override
    protected Class<SundayAccount> getEntityClass() {
        return SundayAccount.class;
    }

    //新增和修改，新增保证是默认账户
    public void saveAccouunt(Long accountId,Long memberId,String name,String bankName,String accountNumber,Integer type,String mobile,String code){
        Map<String,Object> map = new HashMap<>();
        map.put("name",bankName);

        int check_code = codeService.checkCode(mobile,code, SundayCommonConstants.validate_code_type_update_bank);
        if(check_code==0){
            throw new ErrorException("验证码错误。");
        }
        List<SystemDictionary> dictionaries = dicService.list(map);
        String picture = ListUtil.isNotNull(dictionaries)?dictionaries.get(0).getPicture():null;

        if (LongUtil.isNotZreo(accountId)){
            super.updateExceptEmpty(accountId,new SundayAccount(bankName,type,name,accountNumber,picture,mobile));
        }else {

            //新增账户为默认账户，找到以前的账户，全部设置为不默认账户
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("memberId",memberId);
            List<SundayAccount> oldAccounts = super.list(param);
            for (SundayAccount oldAccount:oldAccounts){
                oldAccount.setIsDefault(SundayCommonConstants.NO);
            }
            super.batchSave(oldAccounts);
            //保存本次账户
            super.save(new SundayAccount(memberId,bankName,type,name,accountNumber,picture,SundayCommonConstants.YES,mobile));
        }
    }

    //获取默认地址
    public SundayAccount getDefaultAccount(Long memberId) {
        Map<String,Object> param = new HashMap<>();
        param.put("isDefault",SundayCommonConstants.YES);
        param.put("memberId",memberId);
        List<SundayAccount> accounts = super.list(param);

        if (accounts != null && accounts.size() > 0) {
            return accounts.get(0);
        }
        return null;
    }

    //设置默认地址
    public void setDefaultAccount(Long accountId,Long memberId){
        Map<String,Object> param = new HashMap<>();
        param.put("memberId",memberId);
        List<SundayAccount> accounts = super.list(param);
        for (SundayAccount account:accounts){
            if (account.getId()==accountId){
                account.setIsDefault(SundayCommonConstants.YES);
            }else {
                account.setIsDefault(SundayCommonConstants.NO);
            }
        }
        super.batchSave(accounts);
    }
    //删除地址
    public void deleteAccount(Long accountId){
        SundayAccount add = super.getById(accountId);
        if (add.getIsDefault()==SundayCommonConstants.YES){
            throw new ErrorException("默认地址不能删除");
        }
        super.update(accountId,"isDeleted",SundayCommonConstants.YES);
    }

    public SundayAccount findOne(Long memberId){
        SundayAccount sundayAccount = null;
        sundayAccount=super.getById(memberId);
        return  sundayAccount;
    }

}
